package com.ymatou.iapi.app.keyword.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.app.keyword.parameter.CateBean;
import com.ymatou.iapi.app.keyword.parameter.CateProBean;
import com.ymatou.iapi.app.keyword.service.CateCall;
import com.ymatou.iapi.app.keyword.service.CateProCall;
import com.ymt.base.BaseBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.SearchWapper;

public class Ts_Cate extends BaseBean{
	private static CateBean cateBean;
	private static CateCall cateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile(" 首页分类列表api接口");
	}

	@Before
	public void caseUp() {
		cateBean=new CateBean();
		cateCall=new CateCall();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cateCall.getOpurl());
		//Logger.generateResult();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Cate_001() {
		Logger.start(true,"首页分类商品接口- 验证接口返回公共数据");
		
		try {
			//设置参数
			cateBean.setModule("homepage_cate");
			cateBean.setAppkey(1);
			cateBean.setTimeStamp("0");
			cateBean.setPlatform("pc");
			//cateBean.setFilter("term(fparentcategoryarrayids,1211");
			cateBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateBean.setLimit("0,100");
			cateBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateBean.setExplain("true");
			
			cateCall.setData(cateBean);
			cateCall.callService();
			
			JSONArray hits = cateCall.getHits();
			
			Logger.verifyEquals("OK",cateCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(cateCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, cateCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, cateCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(cateCall.getCommonInfo("hostip"), "服务器ip不为空");
			Logger.verifyNotNull(cateCall.getCommonInfo("requestid"), "requestid不为空");
			Logger.verifyNotNull(cateCall.getCommonInfo("algorithm"), "algorithm不为空");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Cate_002() {
		Logger.start(true,"首页分类商品接口- 验证接口返回的类目是否重复");
		
		try {
			//设置参数
			cateBean.setModule("homepage_cate");
			cateBean.setAppkey(1);
			cateBean.setTimeStamp("0");
			cateBean.setPlatform("pc");
			//cateBean.setFilter("term(fparentcategoryarrayids,1211");
			cateBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateBean.setLimit("0,100");
			cateBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateBean.setExplain("true");
			
			cateCall.setData(cateBean);
			cateCall.callService();
			
			JSONArray hitsAry = cateCall.getHits();
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String id = json.getString("cateId");
				
				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的cateId存在重复值" + id);

				}else{
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的cateId不存在重复值" + id);
					list.add(id);
				}
			
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Cate_003() {
		Logger.start(true,"首页分类商品接口- 验证返回的类目数据与数据库是否一致");
		
		try {
			//设置参数
			cateBean.setModule("homepage_cate");
			cateBean.setAppkey(1);
			cateBean.setTimeStamp("0");
			cateBean.setPlatform("pc");
			//cateBean.setFilter("term(fparentcategoryarrayids,1211");
			cateBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateBean.setLimit("0,100");
			cateBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateBean.setExplain("true");
			
			cateCall.setData(cateBean);
			cateCall.callService();
			
			JSONArray hitsAry = cateCall.getHits();
			//链接数据库返回类目
			
			List cateIdInfo = new  SearchWapper().getCateId(null); 
			JSONArray cateIdInfoArray = new JSONArray(cateIdInfo);
			int infoSize = cateIdInfo.size();
			
			int ifHalfPrice=0;
			for(int i=1;i<=hitsAry.length();i++){
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String cateId = json.getString("cateId");
				if(cateId.equals("-1")){
					ifHalfPrice=1;
				}
			}
			
			int total = hitsAry.length();
			//半价活动的类目只是暂时使用，数据库中并未保存对应的类目，所以不进入以后的比较中
			if(ifHalfPrice==1){
				total = total-1;
			}
			Logger.verifyEquals(infoSize, total, "数据表中查出数据总记录和接口返回记录数一致");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Cate_004() {
		Logger.start(true,"首页分类商品接口- 验证返回的类目类型是否为APP_OPT_CAT");
		
		try {
			//设置参数
			cateBean.setModule("homepage_cate");
			cateBean.setAppkey(1);
			cateBean.setTimeStamp("0");
			cateBean.setPlatform("pc");
			//cateBean.setFilter("term(fparentcategoryarrayids,1211");
			cateBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateBean.setLimit("0,100");
			cateBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateBean.setExplain("true");
			
			cateCall.setData(cateBean);
			cateCall.callService();
			
			JSONArray hitsAry = cateCall.getHits();
			
			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				//得到接口返回中的cateId值
				String cateId = json.getString("cateId");
				
				//根据cateid查询数据库中该类目的类型数据
				if (!cateId.equals("-1")) {
					List cateIdInfo = new  SearchWapper().getCateId(cateId); 
					JSONArray cateIdInfoArray = new JSONArray(cateIdInfo);
					
					Logger.verifyEquals(1, cateIdInfoArray.length(), "查找cateId为："+cateId+"的类目在数据库中是否存在");
				}
				
			}		
			
		} catch (Exception e) {
			Logger.fail(e); 
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Cate_005() {
		Logger.start(true,"首页分类商品接口- 验证返回的类目名称是否正确");
		
		try {
			//设置参数
			cateBean.setModule("homepage_cate");
			cateBean.setAppkey(1);
			cateBean.setTimeStamp("0");
			cateBean.setPlatform("pc");
			//cateBean.setFilter("term(fparentcategoryarrayids,1211");
			cateBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateBean.setLimit("0,100");
			cateBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateBean.setExplain("true");
			
			cateCall.setData(cateBean);
			cateCall.callService();
			
			JSONArray hitsAry = cateCall.getHits();
			
			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				//得到接口返回中的cateId值
				String cateId = json.getString("cateId");
				
				//根据cateid查询数据库中该类目的类型数据
				if (!cateId.equals("-1")) {
					List cateIdInfo = new  SearchWapper().getCateId(cateId); 
					JSONArray cateIdInfoArray = new JSONArray(cateIdInfo);
					
					String cateidString = cateIdInfoArray.get(0).toString();
					JSONObject cateidJsonObject = new JSONObject(cateidString);
					//得到cateName
					String cateName = cateidJsonObject.getString("cateName");
					
					Logger.verifyEquals(cateName, json.getString("cateName"), "查找cateName为："+cateName+"的类目名在数据库中是否存在");
					
				}
				
			}		
			
		} catch (Exception e) {
			Logger.fail(e); 
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Cate_006() {
		Logger.start(true,"首页分类商品接口- 验证返回的类目是否有效");
		
		try {
			//设置参数
			cateBean.setModule("homepage_cate");
			cateBean.setAppkey(1);
			cateBean.setTimeStamp("0");
			cateBean.setPlatform("pc");
			//cateBean.setFilter("term(fparentcategoryarrayids,1211");
			cateBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateBean.setLimit("0,100");
			cateBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateBean.setExplain("true");
			
			cateCall.setData(cateBean);
			cateCall.callService();
			
			JSONArray hitsAry = cateCall.getHits();
			
			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				//得到接口返回中的cateId值
				String cateId = json.getString("cateId");
				
				//根据cateid查询数据库中该类目的类型数据
				if (!cateId.equals("-1")) {
					List cateIdInfo = new  SearchWapper().getCateId(cateId); 
					JSONArray cateIdInfoArray = new JSONArray(cateIdInfo);
					
					String cateidString = cateIdInfoArray.get(0).toString();
					JSONObject cateidJsonObject = new JSONObject(cateidString);
					//得到cateName
					String cateName = cateidJsonObject.getString("cateName");
					
					Logger.verifyEquals(cateName, json.getString("cateName"), "查找cateName为："+cateName+"的类目名在数据库中是否存在");
					
				}
				
			}		
			
		} catch (Exception e) {
			Logger.fail(e); 
		}
	}
	
	//标签排序
	//是否为有效标签
	//段标签类型是否为APP_OPT_CAT
}
